import Web3 from 'web3'
import crypto from 'crypto'
import client from './src/utils/web3util.js'
import logger from './src/utils/logger.js'

const QUICKNODE_RPC_URL ='https://bsc-dataseed.binance.org/' // BSC 主网 RPC 节点

async function main(){
    // const address = '0xAB40Ef2D3dE890693dd82630F490654f09a54dB5';//test
    // const address = '0xB060038e70D81fA17CB18C4D563d266d1FF71299';//seo zf
    // const address = '0xe610ab3156861e9c7b70666aad09c7af4d52cb2e';//seo zf
    // const address = '0x51d887E0788D05325F376F4149e871Ea98e55169';//seo zf

    // XAUM
    // console.log("domain");
    // encodeSeo("0xd604540564b6F659DE163Ad928E29782185DA2FD"); // z00 domain
    // console.log("_maxSupply");
    // encodeSeo('0x9cc405be8c0187355c0db87791d999c43e5b33c8'); //z00 dev
    // const _domain=unit256ToByte32("85410437220457224213449690974794822691474919059668810414598097397773400351869");
    // console.log("_domain unit256ToByte32:"+_domain);
    // const _maxSupply=unit256ToByte32("111135811011579744021834996379294872507383128603609869471680976908345202654983");
    // console.log("_maxSupply unit256ToByte32:"+_maxSupply)



    // console.log("domain");
    // encodeSeo("0x7f6036a3FeEE72Da30CaC0E6961A7Af7Ea428880"); // z00 domain
    // console.log("_maxSupply");
    // encodeSeo('0x51d887E0788D05325F376F4149e871Ea98e55169'); //z00 dev

    // console.log("domain");
    // encodeSeo("0x7f6036a3FeEE72Da30CaC0E6961A7Af7Ea428880"); // z00 domain
    // console.log("_maxSupply");
    // encodeSeo('0x13518AE60712d06fFeA85AFBff3506839F5073ad'); //z00 dev


    console.log("domain");
    encodeSeo("0xe2ff5cdb4abb088050754e6bbd134dba77c7577b"); // z00 domain
    console.log("_maxSupply");
    encodeSeo('0x238193484E809F9cfC4e17D273Ec9600475aD673'); //z00 dev


    // createWallet(10);

    // const privateKey='0x05d700298fd3a10ce62a2301e6414c8bd73a0dbdfc758af977422adcf27d57fb';
    // const privateKey='0xd8042c44c61686f5a154abb83368fa9ebeef131a3d8e51e8bddce156a8503932';
    // await createDeployWallet(privateKey);
}

/**
 * 增发钱包编码
 * @param {*} address 
 * @returns 
 */
  const encodeSeo= (address) => {
  // 编码地址（注意：encodePacked 会将地址按原样编码为字节数组）
  const addressBytes = Buffer.from(address.slice(2), 'hex');
  const hash = crypto.createHash('sha256').update(addressBytes).digest();
  const encode='0x' + hash.toString('hex');
  console.log(`原地址: ${address}\n加密地址: ${encode}`);
  return encode;
}

/**
 * 增发钱包编码
 * @param {*} address 
 * @returns 
 */
const encodeDomain= (address) => {
  const web3 = new Web3();
  // 编码地址（注意：encodePacked 会将地址按原样编码为字节数组）
  const addressBytes = Buffer.from(address.slice(2), 'hex');
  const hash = crypto.createHash('sha256').update(addressBytes).digest();
  // const encode='0x' + hash.toString('hex');
  const encode = web3.utils.padLeft("0x" + hash, 64);
  console.log(`增发地址: ${address}\n地址HASH: ${hash}\n地址加密: ${encode}`);
  return encode;
}


const maxSupply= () => {
  const web3 = new Web3();
  // 1000000000000000000000000000
  // 同样算出十六进制字符串，再 pad
  // const raw = (1_000_000_000n * 10n ** 8n).toString(16);
  const raw = (1n * 10n ** 11n).toString(16);
  const maxSupply = web3.utils.padLeft("0x" + raw, 64);
  console.log("maxSupply (web3):", maxSupply);
  
}


const unit256ToByte32=(str)=>{
  const web3 = new Web3();
  const bigNumber = new web3.utils.BN(str);
  const bytes32Value = web3.utils.padLeft(bigNumber.toString(16), 64);
  // console.log(bytes32Value);
  return bytes32Value;
}

const initWeb3 = async () => {
  const web3 = new Web3(QUICKNODE_RPC_URL);
  return web3;
}

 const createWallet = async(num) => {
    const web3 = await initWeb3();
    const list=[];
    for(let i=0;i<num;i++){
      const account = web3.eth.accounts.create()
      console.info(`\n地址: ${account.address}`);
      console.info(`私钥: ${account.privateKey}`);
      list.push(account);
    }
    return list;
}

const createDeployWallet = async(privateKey,num=6) => {
    client.init("https://bsc-dataseed.binance.org/",logger);
    const list=[];
    for(let i=1;i<=num;i++){
      const account = await client.createAccount()
      console.info(`钱包：${i}\n${account.address}\n${account.privateKey}\n`);
      list.push(account);
    }
    const mainAccount=client.getAccount(privateKey);
    list.unshift(mainAccount);
    for(let i=0;i<list.length-1;i++){
      const from=list[i];
      const to=list[i+1];
      await client.batchCollectBNB([from.privateKey],to.address)
      console.info(`序号：${i} ${from.address} => ${to.address}\n`);
    }
}


main();